package com.sanpu.kxe.business.card.presenter;

import android.content.DialogInterface;
import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.bean.DefBankOcrOutput;
import com.sanpu.kxe.business.card.contract.UpdateBankSecondContract;
import com.sanpu.kxe.business.card.model.CardModel;
import com.sanpu.kxe.business.card.view.BankCardDetailsActivity;
import com.sanpu.kxe.business.card.view.UpdateBankSecondActivity;
import com.sanpu.kxe.business.card.view.UpdateDefBankActivity;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.rxservice.RxError;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：修改默认结算卡第二步
 * Created by 薛勇军 on 2019/5/15.
 */

public class UpdateBankSecondPresenter implements UpdateBankSecondContract.Presenter {

    private UpdateBankSecondContract.View mView;

    private CompositeSubscription mSubscription;

    private DefBankOcrOutput mOcrOutput;

    public UpdateBankSecondPresenter(UpdateBankSecondContract.View mView, DefBankOcrOutput ocrOutput) {
        this.mView = mView;
        this.mOcrOutput = ocrOutput;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void modifyDefUrmBank(String bankNo, String redTel) {
        if (TextUtils.isEmpty(bankNo)) {
            ToastUtil.showTextViewPrompt("必须输入银行卡号！");
            return;
        }
        if (TextUtils.isEmpty(redTel)) {
            ToastUtil.showTextViewPrompt("必须输入手机号！");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(redTel)) {
            ToastUtil.showTextViewPrompt("请输入正确的手机号！");
            return;
        }
        if (mOcrOutput == null) {
            ToastUtil.showTextViewPrompt("缺少有效的证件照");
            return;
        }
        mOcrOutput.setCardNo(bankNo);
        mOcrOutput.setReservePhoneNumber(redTel);
        mView.showLoadingDialog(false);
        Subscription subscription = CardModel.getInstance().modifyDefUrmBank(mOcrOutput)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        if (e instanceof RxError) {
                            RxError rxError = (RxError) e;
                            onCommitResult(rxError.getMessage(), false);
                        }
                    }

                    @Override
                    public void onNext(String ocrOutput) {
                        mView.dismissLoadingDialog();
                        onCommitResult("恭喜你修改默认结算卡成功！", true);
                    }
                });
        mSubscription.add(subscription);
    }

    /**
     * 提交成功
     */
    private void onCommitResult(String remark, final boolean isOk) {
        GlobalDialog dialog = new GlobalDialog(mView.getContext())
                .content(remark)
                .headerIcon(isOk ? R.drawable.img_success_result : R.drawable.img_fail_result);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (isOk) {
                    EventBus.getDefault().post(new RefreshBankEvent());
                    AppContextUtil.finishActivity(UpdateBankSecondActivity.class.getName(),
                            UpdateDefBankActivity.class.getName(), BankCardDetailsActivity.class.getName());
                }
            }
        });
        dialog.show();
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
